package c07运行环境与常用类;

public class 对象克隆的方法与步骤 {
	public static void main(String[] args)
			throws CloneNotSupportedException {
		User u1 = new User(29);
		//@4clone得到u1对象的副本。
		User u2 = u1.clone();
		//判断u1、u2是否相同
		System.err.println(u1 == u2);//false！
		//clone对对象里的引用变量只是简单的复制引用，而不是复制对象内容！
		System.err.println(u1.address == u2.address);//true！
	}
}

class Address {
	String detail;

	public Address(String detail) {
		this.detail = detail;
	}
}

//@1实现Cloneable接口
class User implements Cloneable {
	int age;
	Address address;

	public User(int age) {
		this.age = age;
		address = new Address("广州天河");
	}

	//@2通过调用super.clone()来实现clone()方法
	public User clone()
			throws CloneNotSupportedException {
		return (User) super.clone();//@3返回该对象的副本
	}
}

